﻿#ifndef __ASFFILE__H
#define __ASFFILE__H

#include "fifo_buf.h"
#include "rec_write_base.h"
#include "asf_info.h"


class CAsfFile:public CRecWriteBase
{
public:
	CAsfFile(int channel = 0);
	~CAsfFile();
	virtual int Open(const char * dev, uint32_t fileIndex, uint32_t fileSize,int64_t startTime = 0,PSTFS_RECINFO_EXTERN pExternInfo = NULL);
	virtual int OpenRepairFile(REC_FILE_INFO & recInfo,uint32_t fileLen,int64_t startTime = 0); //追加写入修复的文件,节省空间.
	virtual int Close();
	virtual int WriteFrame(uchar_t * data_buf, uint32_t data_size);
	virtual int SaveSpsPps(uchar_t * data_buf, uint32_t data_size);
	virtual int RepairFile(char*  diskName,uint32_t fileIndex,uint64_t fileLBA,uint32_t recSize,int64_t startTime,uint32_t& fileSize,int64_t & endTime);
	int OpenComFile(const char * file,int64_t startTime,PSTFS_RECINFO_EXTERN pExternInfo);		//打开通用的文件
	int CloseComFile();
protected:

	//写入Frame
	int WriteFrameProc(uchar_t * data_buf, uint32_t data_size,bool bKey,int64_t time);
	int  init_data_packet(int many_payload);
	int init_payload_header(uint32_t packet_offset,int many_payload,int64_t presentation_time,int obj_size,bool bKey);

	int repair_file();
	int WriteTOPaket(uchar_t * data_buf, uint32_t data_size);
	void asf_write_final_process();
	void asf_flush_data_packet();
	void asf_write_simple_index_object();
	void init_file_properties_object(int32_t video_maxbitrate);
    void init_header_extension_object(uint32_t filePos,uint32_t destAlignSize);
	void init_content_description_object(const char *title, const char* author, const char *copyright,int headOffset);

	void init_audio_stream_properties_object(uint16_t encoder_format);
	void init_codec_list_object(const char *vname, const char *aname, uint16_t audio_format,int headOffset);

	void init_video_stream_properties_object(int32_t h264_specific_datalen, uint32_t picwidth, uint32_t picheight);
	int init_header_object(const char *device_id, uint32_t videowidth, uint32_t videoheight);
	int asf_write_data_object();
	void init_data_object();
	void record_gps_into_paddingbuf(int32_t No);
	//重建索引
	int  Rebuild_simple_index();
	void Repair_simple_index_object();
	void Repair_data_object();
	int Repair_head_info();
	static uint64_t unix_to_file_time(time_t ti);
protected:
	struct asf_file_properties_object_t  m_file_properties_obj;
	struct asf_header_extension_object_t m_header_extension_obj;
	struct asf_language_list_object_t    m_lang_list_obj;
	struct asf_simple_index_object_t	 m_simple_index_obj;
	struct asf_padding_object_t          m_padding_obj;
	struct asf_content_description_object_t m_content_description_obj;
	struct asf_header_object_t m_header_obj;
	struct asf_video_stream_properties_object_t m_video_stream_properties_obj;
	struct asf_audio_stream_properties_object_t m_audio_stream_properties_obj;
	struct asf_codec_list_onject_t m_codec_list_obj;
	struct asf_data_object_t m_data_obj;
	struct curr_datapacket_status_t m_curr_datapacket_status;
	struct write_datapacket_parameter_t m_g726_write_datapacket_para;
	struct write_datapacket_parameter_t m_write_datapacket_para;

	uint8_t m_content_copyright_buf[100];
	uint8_t m_vencoder_name_buf[100];
	uint8_t m_aencoder_name_buf[100];
	uchar_t * m_specified_data;		//pps,sps信息
	uint32_t  m_codec_specified_size;	//pps 和sps信息长度
	indexentry_t m_asf_curUdx;            //每个关键帧一个索引，
	vector<indexentry_t> m_index_vec;				//索引集合
private:

//	CFile         m_comFile;
	FILE*         m_comFile;
	CFIFO_buf    * m_pFIFO;
	uint32_t m_bOpen;

	uchar_t * m_pPaddingBuf;					//附加信息地址
	uint16_t  m_vedioWidth;			//视频宽度
	uint16_t  m_vedioHeight;		//视频高度

	uchar_t * m_pDataPkt;						//视频数据缓冲
	uint32_t  m_packetSize;                     //数据包的大小
	bool      m_lastframe_flag;
	bool      m_bPacketFull;	//Data packet 满了．
	uint32_t  m_cur_packetno;	//当前打包编号
	uint32_t  m_data_pkt_len; //		= (128<<10);
	uint16_t  m_pkt_duration;
	uint32_t  m_vedio_sendtm; //视频的发送时间
	uint32_t  m_audio_sendtm; //音频的发送时间;
	uint32_t  m_last_present_time; // present_time
	int       m_STREAM_NUMBER; //流编号类型
	bool      m_bComFile;	  //是否为通用文件
	uint32_t  m_payLoadNu;
};



#endif

